package com.hmall.search.listener;

import com.hmall.search.constants.MqConstants;
import com.hmall.search.service.SearchService;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class MqListener {
    @Autowired
    SearchService service;

    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = MqConstants.UP_QUEUE),
                                            exchange = @Exchange(name = MqConstants.HMALL_EXCHANGE),
                                            key = MqConstants.UP_KEY
    ))
    public void listenUpQueue(String jsonStr) throws IOException {
        service.addDoc(jsonStr);
    }
    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = MqConstants.DOWN_QUEUE),
                                            exchange = @Exchange(name = MqConstants.HMALL_EXCHANGE),
                                            key = MqConstants.DOWN_KEY
    ))
    public void listenDownQueue(String id) throws IOException {
        service.deleteDoc(id);
    }
}
